package com.exam.controller;

import com.exam.aop.MyLog;
import com.exam.dto.ActionResult;
import com.exam.po.*;
import com.exam.service.StudentService;
import com.exam.utils.ApiResultHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XueWanglin
 * @ClassName StudentController
 * @Description TODO
 * @Date 2021/1/11 20:15
 * @Version 1.0
 */
@RestController
@Api(tags = "学生页面的相关操作")
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 根据学生id页码和每页数据容量来查询学生成绩列表
     * @param page 要查询的页码
     * @param pagesize 每页的数据容量
     * @param studentId 学生id
     * @return
     */
    @ApiOperation(value = "根据学生id，页码，每页数据条数进行查询学生成绩")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "要查询的页码",required = true),
            @ApiImplicitParam(name = "pagesize",value = "每页的数据容量",required = true),
            @ApiImplicitParam(name = "studentId",value = "学生id",required = true)
    })
    @MyLog(name = "getScores")
    @RequestMapping(value = "/api/score/{page}/{pagesize}/{studentId}", method = RequestMethod.GET)
    public ActionResult getScores(@PathVariable(name = "page", required = true) Integer page,
                                  @PathVariable(name = "pagesize", required = true) Integer pagesize,
                                  @PathVariable(name = "studentId", required = true) Integer studentId){
//        log.info(String.valueOf(studentId));
        Map<String,Object> map = new HashMap<String,Object>();
        int total = studentService.getScoresCountById(studentId);
        List<Score> list = studentService.getScoresByStudentId(page,pagesize,studentId);
        map.put("total",total);
        map.put("List",list);
        if (map!=null){
            return new ActionResult(200,"success",map);
        }else {
            return ApiResultHandler.buildActionResult(404, "查询失败!!!", null);
        }
    }

    /**
     * 根据学生id更改密码
     * @param student 只包含密码和学生id的实体
     * @return
     */
    @ApiOperation(value = "根据学生id更改密码")
    @ApiImplicitParam(name = "student", value = "只有学生id和新密码的学生实体对象", required = true)
    @RequestMapping(value = "/api/studentPWD", method = RequestMethod.PUT)
    @MyLog(name = "updatePwdByStudentId")
    public ActionResult updatePwdByStudentId(@RequestBody Student student){
//        System.out.println(student.getStudentid());
        boolean flag = studentService.updatePwdByStudentId(student);
        if (flag){
            return new ActionResult(200,"success",null);
        }else {
            return new ActionResult(500,"fail",null);
        }
    }

    /**
     * 分页查询留言和论坛信息
     * @param page 查询的页码
     * @param pagesize 每页数据容量
     * @return
     */
    @ApiOperation(value = "分页查询论坛信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询的页码", required = true),
            @ApiImplicitParam(name = "pagesize", value = "页面数据容量", required = true)
    })
    @RequestMapping(value = "/api/messages/{page}/{pagesize}", method = RequestMethod.GET)
    @MyLog(name = "getMessagesList")
    public ActionResult getMessagesList(@PathVariable(name = "page",required = true) Integer page,
                                        @PathVariable(name = "pagesize",required = true) Integer pagesize){
        Map<String ,Object> map = new HashMap<>();
        int total = studentService.getMessagesCount();
        List<Message> list = studentService.getMessagesList(page,pagesize);
        map.put("total",total);
        map.put("List",list);
        if (map!=null){
            return new ActionResult(200,"success",map);
        }else {
            return ApiResultHandler.buildActionResult(404, "查询失败!!!", null);
        }
    }

    /**
     * 添加回复
     * @param replay 回复实体
     * @return
     */
    @ApiOperation(value = "添加回复")
    @ApiImplicitParam(name = "replay", value = "回复内容实体", required = true)
    @RequestMapping(value = "/api/replay", method = RequestMethod.POST)
    @MyLog(name = "addReplay")
    public ActionResult addReplay(@RequestBody Replay replay){
        Boolean flag = studentService.addReplay(replay);
//        System.out.println(replay.getMessageid());
        if (flag){
            return new ActionResult(200,"success",null);
        }
        return new ActionResult(403,"fail",null);
    }

    /**
     * 分页查询考试数据
     * @param page 要查询的页码
     * @param pagesize 每页的数据容量
     * @return
     */
    @ApiOperation(value = "分页查询考试数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", required = true),
            @ApiImplicitParam(name = "pagesize", value = "每页数据容量", required = true)
    })
    @RequestMapping(value = "/api/exams/{page}/{pagesize}", method = RequestMethod.GET)
    @MyLog(name = "getExamsList")
    public ActionResult getExamsList(@PathVariable(name = "page",required = true) Integer page,
                                 @PathVariable(name = "pagesize",required = true) Integer pagesize){
        Map<String,Object> map = new HashMap<String,Object>();
        int total = studentService.getExamsCount();
        List<ExamMessage> list = studentService.getExams(page,pagesize);
        map.put("total",total);
        map.put("List",list);
        if (map!=null){
            return new ActionResult(200,"success",map);
        }else {
            return ApiResultHandler.buildActionResult(404, "查询失败!!!", null);
        }
    }

    /**
     * 通过关键字查询考试信息
     * @param source 关键字
     * @return
     */
    @ApiOperation(value = "根据考试名进行模糊查询")
    @ApiImplicitParam(name = "source", value = "含有关键字的字段",required = true)
    @RequestMapping(value = "/api/exams", method = RequestMethod.GET)
    @MyLog(name = "getOneExamByKey")
    public ActionResult getOneExamByKey(String source){
//        log.info(source);
        Map<String,Object> map = new HashMap<String,Object>();
        int total = studentService.getExamsCountByKey(source);
        List<ExamMessage> list = studentService.getOneExamByKey(source);
        map.put("total",total);
        map.put("List",list);
        if (map!=null){
            return new ActionResult(200,"success",map);
        }else {
            return ApiResultHandler.buildActionResult(404, "查询失败!!!", null);
        }
    }

    /**
     * 添加小论坛信息
     * @param message message实体对象
     * @return
     */
    @ApiOperation(value = "增加小论坛信息")
    @ApiImplicitParam(name = "message", value = "没有id的message对象（json格式）", required = true)
    @RequestMapping(value = "/api/message",method = RequestMethod.POST)
    @MyLog(name = "insertNewMessage")
    public ActionResult insertNewMessage(@RequestBody Message message){
        Boolean flag = studentService.insertNewMessage(message);
        if (flag){
            return new ActionResult(200,"success",null);
        }
        return new ActionResult(403,"fail",null);
    }

    /**
     * 通过examCode查询考试详细信息
     * @param examCode 要查询的考试编号
     * @return
     */
    @ApiOperation(value = "通过examCode获取考试详细内容")
    @ApiImplicitParam(name = "examCode", value = "要查询的考试编号",required = true)
    @RequestMapping(value = "/api/exam/{examCode}", method = RequestMethod.GET)
    @MyLog(name = "getOneExamMessageByExamCode")
    public ActionResult getOneExamMessageByExamCode(@PathVariable(name = "examCode",required = true) Integer examCode){
        ExamMessage examMessage = studentService.getOneExamMessageByExamCode(examCode);
        if (examMessage!=null){
            return new ActionResult(200,"success",examMessage);
        }
        return new ActionResult(404,"fail",null);
    }

    /**
     * 通过paperId获取试卷详细内容
     * @param paperId 要获取的试卷Id
     * @return
     */
    @ApiOperation(value = "通过paperId获取试卷详细内容")
    @ApiImplicitParam(name = "paperId",value = "要获取的试卷的id",required = true)
    @RequestMapping(value = "/api/paper/{paperId}",method = RequestMethod.GET)
    @MyLog(name = "getPaperAndQueByPaperId")
    public ActionResult getPaperAndQueByPaperId(@PathVariable(name = "paperId",required = true) Integer paperId){
        List<List<PaperMessage>> list = studentService.getPaperAndQueByPaperId(paperId);
        return new ActionResult(200,"success",list);
    }

    /**
     * 学生向老师提出异议
     * @param score
     * @return
     */
    @ApiOperation(value = "学生向老师提出异议")
    @ApiImplicitParam(name = "score", value = "只有scoreid和status的实体对象", required = true)
    @MyLog(name = "updateScoreStatusByScoreId")
    @RequestMapping(value = "/api/objection", method = RequestMethod.PUT)
    public ActionResult updateScoreStatusByScoreId(@RequestBody Score score){
        Boolean flag = studentService.updateScoreStatusByScoreId(score);
        if (flag){
            return new ActionResult(200,"success",null);
        }
        return new ActionResult(403,"fail",null);
    }

    /**
     * 学生提交考试成绩
     * @param score 考试成绩实体
     * @return
     */
    @ApiOperation(value = "学生提交新的考试成绩")
    @ApiImplicitParam(name = "score",value = "考试成绩实体",required = true)
    @MyLog(name = "insertNewTestResult")
    @RequestMapping(value = "/api/score",method = RequestMethod.POST)
    public ActionResult insertNewTestResult(@RequestBody Score score){
        Boolean flag = studentService.insertNewTestResult(score);
        if (flag){
            return new ActionResult(200,"success",null);
        }
        return new ActionResult(403,"fail",null);
    }
}
